﻿using System;
using System.IO;
using Presentation;
using System.Reflection;
using System.Diagnostics;

namespace Infrastructure
{
    public class LoggerTxtFile: ILogger
    {
        public string logFileName;

        public LoggerTxtFile(string fileName)
        {
            logFileName = fileName;
            System.IO.StreamWriter LOG;
            LOG = System.IO.File.CreateText(logFileName);
            LOG.Close();
        }

        public string getLogFileName()
        {
            return this.logFileName;
        }

        public void Write(string text)
        {
            System.IO.StreamWriter LOG;
            LOG = System.IO.File.AppendText(logFileName);
            StackFrame stackFrame = new StackFrame();
            MethodBase methodBase = stackFrame.GetMethod();
            LOG.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " : " + methodBase.Name + " : " + text );
            LOG.Close();
        }

        public string Read()
        {
            var text = "";
            using (var logReader = new StreamReader(logFileName))
            {
                while (!logReader.EndOfStream)
                {
                    text += logReader.ReadLine() + @"\n";
                }
                logReader.Close();
            }
            return text;
        }
    }
}
